package net.oschina.renyong.query.generator.po;

import java.util.*;

import com.baomidou.mybatisplus.generator.config.ConstVal;

import net.oschina.renyong.query.generator.plugin.ICountSqlOptimizer;

/**
 * 一条查询的内容信息<br>
 * 是文件模版生成时需要的内容信息
 * @author Timothy
 */
public class ContentQuery {

	/** 查询总数sql的优化器 */
	private ICountSqlOptimizer countSqlOptimizer;

	/** 查询对应的实体类名 */
	private String entityName;
	/** 查询的注释信息 */
	private String comment;
	/** 查询的SQL */
	private String viewSql;
	/** 查询总数的SQL */
	private String countSql;
	/** Entity类的导入包集合 */
	private List<String> importPackages = new ArrayList<>();
	/** select字段内容集合 */
	private List<ContentField> selectFieldList;
	/** 非select字段信息集合 */
	private List<ContentField> noSelectFieldList;

	/**
	 * 构造方法
	 */
	public ContentQuery() {
	}

	/**
	 * 构造方法
	 * @param countSqlOptimizer 查询总数sql的优化器，可为null
	 */
	public ContentQuery(ICountSqlOptimizer countSqlOptimizer) {
		this.countSqlOptimizer = countSqlOptimizer;
	}

	public String getEntityName() {
		return entityName;
	}

	public ContentQuery setEntityName(String entityName) {
		this.entityName = entityName;
		return this;
	}

	public String getMapperName() {
		return entityName + ConstVal.MAPPER;
	}

	public String getXmlName() {
		return entityName + ConstVal.MAPPER;
	}

	public String getServiceName() {
		return "I" + entityName + ConstVal.SERVICE;
	}

	public String getServiceImplName() {
		return entityName + ConstVal.SERVICE_IMPL;
	}

	public String getComment() {
		return comment;
	}

	public ContentQuery setComment(String comment) {
		this.comment = comment;
		return this;
	}

	public String getViewSql() {
		return viewSql;
	}

	public ContentQuery setViewSql(String viewSql) {
		this.viewSql = viewSql;
		if (this.viewSql != null) {
			this.viewSql = this.viewSql.trim();
		}
		return this;
	}

	public String getCountSql() {
		return countSql;
	}

	public ContentQuery setCountSql(String countSql) {
		this.countSql = countSql;
		if (this.countSql != null) {
			if (this.countSqlOptimizer != null) {
				this.countSql = this.countSqlOptimizer.optimizeCountSql(this.countSql);
			}
			this.countSql = this.countSql.trim();
		}
		return this;
	}

	public List<String> getImportPackages() {
		return importPackages;
	}

	public List<ContentField> getSelectFieldList() {
		return selectFieldList;
	}

	public ContentQuery setSelectFieldList(List<ContentField> selectFieldList) {
		this.selectFieldList = selectFieldList;
		// 收集导入包信息
		Set<String> pkgSet = new TreeSet<>();
		pkgSet.add("java.io.Serializable");
		for (ContentField selectField : selectFieldList) {
			if (null != selectField.getColumnType() && null != selectField.getColumnType().getPkg()) {
				pkgSet.add(selectField.getColumnType().getPkg());
			}
		}
		if (!pkgSet.isEmpty()) {
			this.importPackages = new ArrayList<>(Arrays.asList(pkgSet.toArray(new String[] {})));
		}
		return this;
	}

	public List<ContentField> getNoSelectFieldList() {
		return noSelectFieldList;
	}

	public ContentQuery setNoSelectFieldList(List<ContentField> noSelectFieldList) {
		this.noSelectFieldList = noSelectFieldList;
		return this;
	}
}
